package com.socialcoco.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.socialcoco.config.AliOssCloudUtil;
import com.socialcoco.entity.*;
import com.socialcoco.respone.BaseResponse;
import com.socialcoco.respone.ResultUtils;
import com.socialcoco.respone.resultenum;
import com.socialcoco.serviceImpl.ChatServiceImpl;
import com.socialcoco.serviceImpl.DiaryImageServiceImpl;
import com.socialcoco.serviceImpl.DiaryServiceImpl;
import com.socialcoco.serviceImpl.UserServiceImpl;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@Slf4j
@RestController
@Api(tags = "用户功能")
@RequestMapping("/socialcoco/user")
public class OssController {
    @Autowired
    private AliOssCloudUtil aliOssCloudUtil;
    @Autowired
    private DiaryServiceImpl diaryService;
    @Autowired
    private DiaryImageServiceImpl diaryImageService;
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ChatServiceImpl chatService;
    @PostMapping("/uploadimg")
    @ApiOperation(value = "用户发表动态，多文件MultipartFile上传")
    @ApiImplicitParams({@ApiImplicitParam(name = "file[]", value = "文件流对象,接收数组格式", required = false,dataType = "MultipartFile",allowMultiple = true),
            @ApiImplicitParam(name = "title", value = "title", required = true)}
    )
    public BaseResponse<Object> uploadOssFile(@RequestParam(value = "file",required = false) MultipartFile files,@RequestHeader("token")String token,@RequestParam(value = "did",required = false)String did) throws Exception{
        if (files != null) {
            String url = aliOssCloudUtil.uploadFile(files);
            DiaryImage diaryImage = new DiaryImage();
            diaryImage.setDid(Integer.valueOf(did));
            diaryImage.setTypes(url.substring(url.lastIndexOf(".") + 1));
            diaryImage.setImage(url);
            diaryImageService.save(diaryImage);
        }
        return ResultUtils.success("ok");
    }
    @PostMapping("/SaveDiary")
    @ApiOperation(value = "用户发表动态文字")
    public BaseResponse<Object> SaveDiary(@RequestBody Diary diary,@RequestHeader("token")String token){
        Integer id=Math.abs((int) System.currentTimeMillis());
        diary.setId(id);
        diary.setCreateTime(new Date());
        diaryService.save(diary);
        return ResultUtils.success(id);
    }
    @ApiOperation("用户修改头像")
    @PostMapping("/modifyImage")
    public BaseResponse<Object> modifyImage(@RequestParam(value = "file",required = false) MultipartFile files,
                                            @RequestParam("did") String user, @RequestHeader("token")String token){
        if (files != null) {
            User user1;
            user1= (User) JSONObject.toBean(JSONObject.fromObject(user),User.class);
            String url = aliOssCloudUtil.uploadFile(files);
            user1.setHeadimgae(url);
            userService.updateById(user1);
            log.info(user1.toString());
            Map map = new HashMap();
            map.put("data",user1);
            return ResultUtils.success(user1);
        }
        return ResultUtils.error(resultenum.SERVERERROR.getCode(),resultenum.SERVERERROR.getMsg());

    }
    @ApiOperation("用户发送图片消息")
    @PostMapping("/sendImage")
    public BaseResponse<Object> sendImage(@RequestParam(value = "file",required = false) MultipartFile files,@RequestParam("did")String message,@RequestHeader("token")String token){
        Chat chat;
        chat= (Chat) JSONObject.toBean(JSONObject.fromObject(message),Chat.class);
        if (files != null) {
            String url = aliOssCloudUtil.uploadFile(files);
            chat.setContent(url);
            chat.setType(1);
            chat.setActive(0);
            chat.setCreateTime(new Date());
            chatService.save(chat);
            return ResultUtils.success("imageOk");
        }
        return ResultUtils.error(resultenum.SERVERERROR.getCode(),resultenum.SERVERERROR.getMsg());
    }
}